package com.laughing.lib.base

import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar

fun HBaseFragment<*, *>.setTitle(titleId: Int) {
    val titleBar = binding.root.findViewById<TitleBar>(R.id.titleBar) ?: return
    titleBar.setTitle(titleId)
}

fun HBaseFragment<*, *>.setTitle(title: String) {
    val titleBar = binding.root.findViewById<TitleBar>(R.id.titleBar) ?: return
    titleBar.title = title
}

fun HBaseFragment<*, *>.setOnTitleBarListener(listenerBuilder: OnTitleBarListenerBuilder.() -> Unit) {
    val titleBar = binding.root.findViewById<TitleBar>(R.id.titleBar) ?: return
    val listener = OnTitleBarListenerBuilder().also(listenerBuilder)
    titleBar.setOnTitleBarListener(listener)
}

class OnTitleBarListenerBuilder : OnTitleBarListener {
    var leftClick: (titleBar: TitleBar) -> Unit = {}
    var rightClick: (titleBar: TitleBar) -> Unit = {}
    var titleClick: (titleBar: TitleBar) -> Unit = {}
    override fun onLeftClick(titleBar: TitleBar) {
        super.onLeftClick(titleBar)
        leftClick(titleBar)
    }

    override fun onRightClick(titleBar: TitleBar) {
        super.onRightClick(titleBar)
        rightClick(titleBar)
    }

    override fun onTitleClick(titleBar: TitleBar) {
        super.onTitleClick(titleBar)
        titleClick(titleBar)
    }
}